//
//  KZTimeCountDownView.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/10/22.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "KZTimeCountDownView.h"


@implementation KZTimeCountDownView
{
    UIView *_bgView;
    CAShapeLayer *_ringTimeCountDownLayer;      // 环形倒计时

    
    KZTimeCountDownContentType _contentType;

}

- (instancetype)initWithContentType:(KZTimeCountDownContentType)type {
    if (self = [super init]) {
        
        self.backgroundColor = UIColor.whiteColor;
        [self setupUI];
        [self setType:type];
    }
    return self;
}

- (instancetype)initWithContentType:(KZTimeCountDownContentType)type withTime:(NSInteger)totalTime {
    if (self = [super init]) {
        
        self.backgroundColor = UIColor.whiteColor;
        [self setupUI];
        [self setType:type withtotalTime:totalTime];
    }
    return self;
}

- (void)setType:(KZTimeCountDownContentType)type {
    
    _contentType = type;
    switch (_contentType) {
        case KZTimeCountDownContentTypePlayVoice:
        {
            _totalTime = 3;
            [self setupPlayVoiceUI];
        }
            break;
        case KZTimeCountDownContentTypeRecordingFirst:
        {
            _totalTime = 5;
            [self setupRecordingFirstUI];
        }
            break;
        case KZTimeCountDownContentTypeRecordingAgain:
        {
            _totalTime = 3;
            [self setupRecordingAgainUI];
        }
            break;
            
        default:
            break;
    }
}

- (void)setType:(KZTimeCountDownContentType)type withtotalTime:(NSInteger)totalTime {
    _contentType = type;
    switch (_contentType) {
        case KZTimeCountDownContentTypePlayVoice:
        {
            _totalTime = totalTime;
            [self setupPlayVoiceUI];
        }
            break;
        case KZTimeCountDownContentTypeRecordingFirst:
        {
            _totalTime = totalTime;
            [self setupRecordingFirstUI];
        }
            break;
        case KZTimeCountDownContentTypeRecordingAgain:
        {
            _totalTime = totalTime;
            [self setupRecordingAgainUI];
        }
            break;
        case KZTimeCountDownContentTypeWait:
        {
            _totalTime = totalTime;
            [self setupWaitingUI];
        }
            break;
        case KZTimeCountDownContentTypeReadytoAnswer:
        {
            _totalTime = totalTime;
            [self setupReadyAnswerUI];
        }
            break;
        default:
            break;
    }
}

- (void)setTime:(CGFloat)time {
    _time = time;
    _ringTimeCountDownLayer.strokeEnd = (_time + 0.1) / _totalTime;
    if (_contentType == KZTimeCountDownContentTypePlayVoice && _time == _totalTime) {
        [_turmpetView stop];
    }
}


- (void)setupUI {
    
    CGFloat lineWidth = W(4);
    
    _bgView = UIView.new;
    [self addSubview:_bgView];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(lineWidth + 2);
        make.right.bottom.mas_equalTo(-lineWidth - 2);
    }];
    
    _ringTimeCountDownLayer = [CAShapeLayer layer];
    _ringTimeCountDownLayer.lineWidth = lineWidth;
    _ringTimeCountDownLayer.lineCap = kCALineCapRound;
    _ringTimeCountDownLayer.fillColor = UIColor.clearColor.CGColor;
    [self.layer addSublayer:_ringTimeCountDownLayer];
    
}
- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    self.layer.cornerRadius = self.width / 2;
    _bgView.layer.cornerRadius = self.width / 2 - _ringTimeCountDownLayer.lineWidth - 2;
    
    // 环形路径
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.width / 2, self.height / 2) radius:(self.width - _ringTimeCountDownLayer.lineWidth) / 2 startAngle:-M_PI/2 endAngle:M_PI/2*3 clockwise:true];
    _ringTimeCountDownLayer.path = path.CGPath;
    _ringTimeCountDownLayer.strokeEnd = 0;
    
    // 阴影
    self.layer.shadowOffset = CGSizeMake(0, 0);
    self.layer.shadowRadius = 15;
    self.layer.shadowOpacity = 0.6;
}

- (void)setupPlayVoiceUI {
    
    _ringTimeCountDownLayer.strokeColor = KZThemeColor2.CGColor;
    self.layer.shadowColor = KZThemeColor2.CGColor;
    _bgView.backgroundColor = KZThemeColor2;
    
    // 喇叭视图
    KZTurmpetView *turmpetView = [KZTurmpetView new];
    [_bgView addSubview:turmpetView];
    [turmpetView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(_bgView.mas_height).multipliedBy(0.7);
        make.center.equalTo(_bgView);
    }];
    _turmpetView = turmpetView;
}
- (void)setupRecordingFirstUI {
    
    _ringTimeCountDownLayer.strokeColor = KZThemeColor.CGColor;
    self.layer.shadowColor = KZThemeColor.CGColor;
    _bgView.backgroundColor = KZThemeColor;
    
    // 喇叭视图
    KZRecordingView *recordingView = [KZRecordingView new];
    [_bgView addSubview:recordingView];
    [recordingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(_bgView.mas_height).multipliedBy(0.5);
        make.center.equalTo(_bgView);
    }];
    _recordingView = recordingView;
}
- (void)setupRecordingAgainUI {
    
    _ringTimeCountDownLayer.strokeColor = KZThemeColor.CGColor;
    self.layer.shadowColor = KZThemeColor.CGColor;
    _bgView.backgroundColor = KZThemeColor;
    
    // 喇叭视图
    KZRecordingView *recordingView = [KZRecordingView new];
    [_bgView addSubview:recordingView];
    [recordingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(_bgView.mas_height).multipliedBy(0.5);
        make.center.equalTo(_bgView);
    }];
    _recordingView = recordingView;
}

- (void)setupWaitingUI {
    _ringTimeCountDownLayer.strokeColor = KZThemeColor.CGColor;
    self.layer.shadowColor = KZThemeColor.CGColor;
    _bgView.backgroundColor = KZThemeColor;
   
    UILabel *titlelabel = [[UILabel alloc] init];
    [_bgView addSubview:titlelabel];
    titlelabel.textAlignment = NSTextAlignmentCenter;
    titlelabel.textColor = [UIColor whiteColor];
    titlelabel.font = FONT(W(16));
    titlelabel.text = @"请准备";
    [titlelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(_bgView.mas_height).multipliedBy(0.8);
        make.center.equalTo(_bgView);
    }];
}

- (void)setupReadyAnswerUI {
    _ringTimeCountDownLayer.strokeColor = KZThemeColor.CGColor;
    self.layer.shadowColor = KZThemeColor.CGColor;
    _bgView.backgroundColor = KZThemeColor;
    
    UILabel *titlelabel = [[UILabel alloc] init];
    [_bgView addSubview:titlelabel];
    titlelabel.textAlignment = NSTextAlignmentCenter;
    titlelabel.textColor = [UIColor whiteColor];
    titlelabel.font = FONT(W(16));
    titlelabel.text = @"请作答";
    [titlelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(_bgView.mas_height).multipliedBy(0.8);
        make.center.equalTo(_bgView);
    }];
  
}

- (void)reset {
    _ringTimeCountDownLayer.strokeEnd = 0;
}


@end
